//
//  CYShopViewController.m
//  CreditAddressBook
//
//  Created by Mosu&Cuu on 15/8/31.
//  Copyright (c) 2015年 Lee. All rights reserved.
//

#import "CYShopViewController.h"
#import "CreditWebView.h"
#import "CreditNavigationController.h"
#import "CreditWebViewController.h"
#import "CYMeHttpTool.h"
#import "CYLoginHttpTool.h"
#import "UIImageView+WebCache.h"
#import "CYUser.h"
#import "CYUserData.h"
#import "MBProgressHUD+MJ.h"
#import "MBProgressHUD.h"
#import "CYJumpsTool.h"
#import "IGLDropDownMenu.h"
#import "CYShopModel.h"
#import "CYShopViewCell.h"
#import "CYTool.h"
#import "CYJsonInfo.h"
#import "MJExtension.h"
#import "TOWebViewController.h"

@interface CYShopViewController ()<UIScrollViewDelegate,UITableViewDelegate,UITableViewDataSource>
{
    UITableView *_shopTabelView ;//下面的tabbleview
     NSMutableArray *_dataArray;//数据源
}

@property (nonatomic,strong) UILabel *textLabel;
@property (nonatomic,strong) UIPageControl *myPageControl;
@property (nonatomic, strong) NSArray *integralTaskInfo;
@property (nonatomic,strong) NSMutableArray *MidArray;
@property (nonatomic,strong) NSMutableArray *ExchangeArr;


@end

@implementation CYShopViewController

@synthesize text;
@synthesize pageControl;


- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupNavigationView];
    [self requestNum];//获取积分
    self.view.backgroundColor = CYColor(193, 216, 191, 1);
    self.title = @"我的任务";
    
    [self getshopmission];
    
    _dataArray = [NSMutableArray array];
    
    UIView *naviView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CYScreeSize.width, 64)];
    naviView.backgroundColor = CYNaviThemeColor;
    [self.view addSubview:naviView];
}

- (void)getshopmission {
    CYUser *user = [CYLoginHttpTool  userData].user;
    if (!user) return;
     [MBProgressHUD showMessage:nil toView:self.view];
    [CYHttpTool postWithURL:[CYURL getDetailMission] params:@{@"userId" :user.userId} success:^(id json) {
        [MBProgressHUD hideHUDForView:self.view];
        CYJsonInfo *jsonInfo = [CYJsonInfo objectWithKeyValues:json];
        if (jsonInfo.isSuccess) {
            NSArray *a = jsonInfo.message;
            for (NSDictionary *dict in a) {
                CYShopModel *model = [[CYShopModel alloc]init];
                [model setValuesForKeysWithDictionary:dict];
                if (model.type != 5) {
                    [_dataArray addObject:model];
                }
            }

            [_shopTabelView reloadData];
        } else {
           CYLog(@"失败:没有数据");
        }
    } failure:^(NSError *error) {
        CYLog(@"失败:%@",error);
        [MBProgressHUD hideHUDForView:self.view];
        [MBProgressHUD showError:CYServerConnectFailure];
    }];
}

- (void)requestNum {
    CYUser *user = [CYLoginHttpTool userData].user;
    [CYHttpTool postWithURL:[CYURL getAllPointByUserId] params:@{@"userId":user.userId} success:^(id json) {
         CYJsonInfo *jsonInfo = [CYJsonInfo objectWithKeyValues:json];
         self.user_point = jsonInfo.message;
        [self creadeUI];
    } failure:^(NSError *error) {
        CYLog(@"更新用户审核资料服务器连接失败：%@", error);
        
    }];
}

- (void)creadeUI {
    //头像
    UIImageView *icon_img = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.x+20, self.view.y+64+20,self.view.width/4, self.view.width/4)];
    icon_img.layer.cornerRadius = icon_img.width/2;
    icon_img.clipsToBounds = YES;
    icon_img.backgroundColor = [UIColor grayColor];
    CYUser *user = [CYLoginHttpTool userData].user;
    if (user.headPic) {
        [icon_img sd_setImageWithURL:CYURLWithUserService(user.headPic) placeholderImage:nil];
    }else if (!user.headPic) {
        icon_img.image = [UIImage imageNamed:@"complete_profile_men_sel"];

    }

    [self.view addSubview:icon_img];
    
    //名字
    UILabel *namelab = [[UILabel alloc]initWithFrame:CGRectMake(icon_img.x,icon_img.y+icon_img.height+10, icon_img.width, 20)];
    namelab.textAlignment = NSTextAlignmentCenter;
    namelab.textColor = [UIColor blackColor];
    namelab.font = [UIFont fontWithName:@"Kokonor" size:14];
    if (user.nickName) {
        namelab.text = user.nickName;
    } else {
        namelab.text = @"--";
    }

    [self.view addSubview:namelab];
    
     UILabel *jifenlab = [[UILabel alloc] initWithFrame:CGRectMake(icon_img.x+icon_img.width+70,icon_img.y+icon_img.height-50, 100, 20)];
    jifenlab.textColor = [UIColor blackColor];
    jifenlab.text = [NSString stringWithFormat:@"剩余积分:%@",self.user_point];
    jifenlab.font = [UIFont fontWithName:@"Futura" size:14];//设置文字类型与大小
    [self.view addSubview:jifenlab];
    //Ingetra_lab.backgroundColor = [UIColor grayColor];

    //任务线
    UILabel *quicklab = [[UILabel alloc]initWithFrame:CGRectMake(-5, icon_img.y+icon_img.height+60, self.view.width+20, 40)];
    quicklab.backgroundColor = [UIColor whiteColor];
    quicklab.textColor = CYColor(131, 195, 111, 1);
    quicklab.font = [UIFont fontWithName:@"Kokonor" size:20];
    quicklab.text = @"做任务得积分";
    quicklab.layer.borderColor = [UIColor whiteColor].CGColor;
    quicklab.layer.borderWidth = 0.8;
    quicklab.textAlignment = NSTextAlignmentCenter;
    //quicklab.backgroundColor = [UIColor grayColor];
    
    UIButton *Shopligonbtn = [UIButton buttonWithType:UIButtonTypeCustom];
    Shopligonbtn.frame = CGRectMake(jifenlab.x,jifenlab.height+jifenlab.y+10, 70, 20);
    Shopligonbtn.backgroundColor = CYColor(131, 195, 111, 1);
    Shopligonbtn.layer.cornerRadius = 5.0f;
    [Shopligonbtn setTitle:@"积分兑换" forState:UIControlStateNormal];
    Shopligonbtn.titleLabel.font = [UIFont systemFontOfSize:14];
    Shopligonbtn.titleLabel.textColor = [UIColor whiteColor];
    Shopligonbtn.showsTouchWhenHighlighted = YES;
    [self.view addSubview:Shopligonbtn];
    [Shopligonbtn addTarget:self action:@selector(enter) forControlEvents:UIControlEventTouchUpInside];
    
    _shopTabelView = [[UITableView alloc]initWithFrame:CGRectMake(0,quicklab.y+1+quicklab.height, self.view.width, self.view.height-quicklab.y-quicklab.height) style:UITableViewStylePlain];
    _shopTabelView.backgroundColor = [UIColor whiteColor];
    _shopTabelView.dataSource = self ;
    _shopTabelView.delegate = self ;
    [_shopTabelView registerNib:[UINib nibWithNibName:@"CYShopViewCell" bundle:nil] forCellReuseIdentifier:@"CYShopViewCell"];
    [self.view addSubview:_shopTabelView];
    
    [self.view addSubview:quicklab];
    
    [CYTool setExtraCellLineHiddenWithTableView:_shopTabelView];
}

#pragma mark - tableview代理
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CYShopViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CYShopViewCell" forIndexPath:indexPath];
    cell.model = _dataArray[indexPath.row];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell ;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60 ;
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
   
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArray.count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    CYShopModel *model = _dataArray[indexPath.row];
    if (model.url) {
        TOWebViewController *webViewController = [[TOWebViewController alloc] initWithURL:CYURLWithString(model.url)];
        webViewController.showUrlWhileLoading = YES;
        webViewController.navigationButtonsHidden = YES;
        webViewController.showPageTitles = NO;
        webViewController.title = model.title;
        [self.navigationController pushViewController:webViewController animated:YES];
    }
}

#pragma mark -
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
           }
    return self;
}

- (void)enter {
    CYUser *user = [CYLoginHttpTool userData].user;
    MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.mode=MBProgressHUDAnimationFade;//枚举类型不同的效果
    hud.labelText=@"加载中...";
    [CYMeHttpTool ShopwithaddressWithParams:@{@"userId": user.userId} success:^(NSString *message) {
        CreditWebViewController *shopwebVC = [[CreditWebViewController alloc] initWithUrl:message];
        shopwebVC.hidesBottomBarWhenPushed = YES;
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [self.navigationController pushViewController:shopwebVC animated:YES];
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
}

@end
